<?php
global $_W, $_GPC;
load()->func('tpl');
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
$userTable = 'hellozjx_mapwxapp_user';

if ($operation == 'display') {
    $pindex = max(1, intval($_GPC['page']));
    $psize = 20;
    $search_phone = trim($_GPC['search_phone']);
    $search_nickname = ltrim($_GPC['search_nickname']);
    $search_status = trim($_GPC['search_status']);
    $condition = '';
    if (!empty($search_phone)) {
        $condition = " AND phone = {$search_phone}";
    }
    if (!empty($search_nickname)) {
        $condition = " AND nick_name like '%{$search_nickname}%'";
    }
    if (!empty($search_status)) {
        $condition = " AND status = $search_status";
    }

    $list = pdo_fetchall("SELECT * FROM " . tablename($userTable) . " where status > 0 {$condition} ORDER BY id DESC LIMIT " . ($pindex - 1) * $psize . ',' . $psize);
    $total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename($userTable) . " WHERE status > 0 {$condition}");
    $pager = pagination($total, $pindex, $psize);
} elseif ($operation == 'post') {
    $id = intval($_GPC['id']);
    if (empty($id))
        message('操作失败!', $this->createWebUrl('user', array('op' => 'display')), 'success');
    if (checksubmit('submit')) {
        $data = array('status' => $_GPC['status']);

        pdo_update($userTable, $data, array('id' => $id));
        message('更新分类成功！', $this->createWebUrl('user', array('op' => 'display')), 'success');
    }
    $user = pdo_fetch("select * from " . tablename($userTable) . " where id={$id} and status > 0 limit 1");
} elseif ($operation == 'delete') {
    $id = intval($_GPC['id']);  $user = pdo_fetch("SELECT id FROM " . tablename($userTablest) . " WHERE id = '{$id}' AND status > 0");
    if (empty($user)) {
        message('抱歉，该用户不存在或是已经被删除！', $this->createWebUrl('user', array('op' => 'display')), 'error');
    }
    pdo_update($userTable, ['status'=>0] , array('id' => $id));
    message('删除成功', referer(), 'success');
}
include $this->template('user');